<script setup>
  import { getFileList, deleteFile } from '@/api/file'
  import { ref } from 'vue'
  import { useAlbumStore } from '@/stores'
  const albumStore = useAlbumStore()
  const dialogVisible = ref(false)
  const pictureList = ref([])
  const queryData = ref({
    pageNum: 1,
    pageSize: 10,
    type: 'image'
  })
  getFileList(queryData.value).then(res => {
    pictureList.value = res.data.records
    sortData()
  })
  const dialogImageUrl = ref('')
  const handlePreview = url => {
    dialogImageUrl.value = url
    dialogVisible.value = true
  }
  const deleteById = id => {
    deleteFile(id).then(() => {
      ElMessage.success('删除成功')
      pictureList.value = pictureList.value.filter(item => item.id !== id)
    })
  }
  const handleUpload = data => {
    pictureList.value.unshift(data)
    ElMessage.success('上传成功')
  }
  const fileInfo = ref(null)
  const openFileInfo = data => {
    fileInfo.value.open(data)
  }
  const handleSort = orderBy => {
    albumStore.setSortInfo(orderBy)
    sortData()
  }
  function sortData() {
    const orderBy = albumStore.sortInfo.orderBy
    if (albumStore.sortInfo.order == 'desc') {
      pictureList.value.sort(function (a, b) {
        if (a[orderBy] < b[orderBy]) {
          return -1
        }
        if (a[orderBy] > b[orderBy]) {
          return 1
        }
        return 0
      })
    } else {
      pictureList.value.sort(function (a, b) {
        if (a[orderBy] > b[orderBy]) {
          return -1
        }
        if (a[orderBy] < b[orderBy]) {
          return 1
        }
        return 0
      })
    }
  }
</script>

<template>
  <FileInfo ref="fileInfo"></FileInfo>
  <div class="album">
    <div class="header">
      <div class="title">
        <span class="icon"></span>
        <span>相册</span>
      </div>
      <div class="right">
        <div
          class="item"
          :class="{ active: albumStore.sortInfo.orderBy == 'updateTime' }"
          @click="handleSort('updateTime')"
        >
          修改日期
          <span v-if="albumStore.sortInfo.orderBy == 'updateTime'">
            <svg
              class="icon flex"
              aria-hidden="true"
              v-if="albumStore.sortInfo.order == 'desc'"
            >
              <use xlink:href="#icon-jiangxu"></use>
            </svg>
            <svg class="icon flex" aria-hidden="true" v-else>
              <use xlink:href="#icon-shengxu"></use>
            </svg>
          </span>
        </div>
        <div
          class="item"
          :class="{ active: albumStore.sortInfo.orderBy == 'name' }"
          @click="handleSort('name')"
        >
          文件名
          <span v-if="albumStore.sortInfo.orderBy == 'name'">
            <svg
              class="icon flex"
              aria-hidden="true"
              v-if="albumStore.sortInfo.order == 'desc'"
            >
              <use xlink:href="#icon-jiangxu"></use>
            </svg>
            <svg class="icon flex" aria-hidden="true" v-else>
              <use xlink:href="#icon-shengxu"></use>
            </svg>
          </span>
        </div>
      </div>
    </div>
    <div class="box">
      <div class="img">
        <Upload @success="handleUpload"></Upload>
      </div>
      <div class="img" v-for="file in pictureList" :key="file.id">
        <el-image :src="file.path" fit="cover" lazy />
        <div class="mask flex-center">
          <svg
            class="icon flex"
            aria-hidden="true"
            @click="handlePreview(file.path)"
          >
            <use xlink:href="#icon-fangdajing"></use>
          </svg>
          <svg class="icon flex" aria-hidden="true" @click="openFileInfo(file)">
            <use xlink:href="#icon-edit"></use>
          </svg>
          <el-popconfirm
            title="真的要删除这张照片吗"
            confirm-button-text="是"
            cancel-button-text="否"
            @confirm="deleteById(file.id)"
          >
            <template #reference>
              <svg class="icon flex" aria-hidden="true">
                <use xlink:href="#icon-shanchu"></use>
              </svg>
            </template>
          </el-popconfirm>
        </div>
      </div>
    </div>
  </div>
  <el-dialog v-model="dialogVisible" center :close-on-click-modal="false" width="50%">
    <img :src="dialogImageUrl" alt="Preview Image" style="width: 100%;" />
  </el-dialog>
</template>

<style lang="scss" scoped>
  .album {
    padding: 0 24px;
    .box {
      margin-top: 10px;
      display: grid;
      justify-content: space-between;
      grid-template-columns: repeat(auto-fill, 150px);
      gap: 10px;
      .img {
        width: 150px;
        height: 150px;
        position: relative;
        font-size: 11px;
        .form {
          display: flex;
          align-items: center;
          .form-name {
            flex-shrink: 0;
            color: var(--color-secondary-label);
          }
          .form-value {
            overflow-x: scroll;
            white-space: nowrap;
            color: var(--color-primary-label);
          }
        }
        &:hover {
          .mask {
            opacity: 1;
          }
        }
        .el-image {
          display: block;
          width: 150px;
          height: 150px;
          border-radius: 5px;
          overflow: hidden;
        }
        .mask {
          color: #fff;
          position: absolute;
          opacity: 0;
          top: 0;
          left: 0;
          width: 150px;
          height: 150px;
          background: rgba(0, 0, 0, 0.5);
          z-index: 1;
          transition: opacity 0.3s;
          border-radius: 5px;
          .icon {
            margin: 5px;
            cursor: pointer;
          }
        }
      }
    }
    .header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      line-height: 28px;
      font-size: 20px;
      font-weight: 900;
      color: var(--color-primary-label);
      .title {
        display: flex;
        align-items: baseline;
      }

      .icon {
        background: var(--el-color-primary);
        position: relative;
        top: 2px;
        display: inline-block;
        width: 6px;
        height: 18px;
        border-radius: 3px;
        margin-right: 2px;
      }
      .right {
        width: 180px;
        height: 30px;
        background: var(--color-active-background);
        padding: 2px 4px;
        border-radius: 999px;
        cursor: pointer;
        display: flex;
        .item {
          width: 90px;
          height: 25px;
          border-radius: 999px;
          background: transparent;
          color: var(--color-tertiary-label);
          text-align: center;
          line-height: 25px;
          font-size: 12px;
          user-select: none;
          .icon {
            width: 12px;
            height: 12px;
            background: none;
            vertical-align: baseline;
          }

          &.active {
            background: var(--elevation-low-background);
            box-shadow: var(--elevation-low-shadow);
            color: var(--color-primary-label);
          }
        }
      }
    }
  }
</style>
